<?php
 
require_once "HTTP/Request.php";
require_once $_SERVER["DOCUMENT_ROOT"]."monotrix/modules/main/prolog.php";

function add_to_log($error, $msg) {
	return;
}
 
function get_ticket($src, $dst, $date, $needtrain) {
	
	$r = new Http_Request();
	$r->setMethod(HTTP_REQUEST_METHOD_POST);
	$r->setURL("http://booking.uz.gov.ua/ru/purchase/search/");
	$r->addPostData('station_id_from',$src);
	$r->addPostData('station_id_till',$dst);
	$r->addPostData('date_start',$date);
	
	if (!PEAR::isError($r->sendRequest())) {
	     $response1 = $r->getResponseBody();
	} else {
	     $response1 = "";
	     return;	     
	}
	$res = json_decode($response1, true);
	if ($res["error"]) {
		echo @date("r")." Error: ".$res["value"]."\n";
		add_to_log(true, $res["value"]);
		return;
	}
	//print_r($res["value"]);
	foreach ($res["value"] as $train) {
		if (preg_match("/".$needtrain."/", $train["num"])) {
			//echo "good";
			//get_last_state();
			return $train["types"];
		}
	}
}
if (CModule::IncludeModule("ticketwatch")) {
	$rsWatchList = CWatch::GetList();
	print_r($rsWatchList);
}
	print_r(get_ticket("2218260", "2200013", "25.09.2012", "074"));
?>

